package com.simas.screen;

import java.io.ByteArrayInputStream;
import java.io.InputStream;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import com.simas.common.CommonFunction;
import com.simas.common.CommonMenu;
import com.simas.common.CustomResources;
import com.simas.common.NoWaitProcess;
import com.simas.common.XMLWaitProcess;
//import com.simas.common.DeletePolisInfo;
import com.simas.component.CellButton;
import com.simas.component.CustomButton;
import com.simas.component.RowButton;
import com.simas.info.CallInfoServlet;
//import com.simas.common.OpenInfoScreen;
//import com.simas.common.OpenPolisScreen;
//import com.simas.common.info.CallInfoServlet;

import net.rim.device.api.system.Bitmap;
import net.rim.device.api.ui.Color;
import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.FieldChangeListener;
//import net.rim.device.api.ui.Font;
//import net.rim.device.api.ui.FontFamily;
//import net.rim.device.api.ui.Graphics;
import net.rim.device.api.ui.Keypad;
//import net.rim.device.api.ui.Manager;
//import net.rim.device.api.ui.Ui;
import net.rim.device.api.ui.UiApplication;
import net.rim.device.api.ui.component.BitmapField;
import net.rim.device.api.ui.component.Dialog;
import net.rim.device.api.ui.component.LabelField;
import net.rim.device.api.ui.component.Menu;
import net.rim.device.api.ui.component.RichTextField;
import net.rim.device.api.ui.container.HorizontalFieldManager;
import net.rim.device.api.ui.container.MainScreen;
import net.rim.device.api.ui.container.VerticalFieldManager;
import net.rim.device.api.ui.decor.Background;
import net.rim.device.api.ui.decor.BackgroundFactory;
import net.rim.device.api.xml.parsers.DocumentBuilder;
import net.rim.device.api.xml.parsers.DocumentBuilderFactory;

public class XmlShowINFOScreen extends MainScreen implements FieldChangeListener {
	CommonFunction cf = new CommonFunction();
	CustomResources cr = new CustomResources();
	private CellButton btHome;
	private CellButton btInfoRS;
	private CellButton btInfoPersh;
	private CellButton btInfoTesti;
	private CellButton btInfoSimas;
	private LabelField lblSeparator1;
	private LabelField lblSeparator2;
	private LabelField lblSeparator3;
//	private LabelField lblSeparator4;
	
	private int intGetDetail = 0; 
	private int intJnsInfo = 0;
	
	private CustomButton btNextList;
	private CustomButton btPrevList;
	private RowButton btInfoTitle;
	private String strFoto;
	
	private VerticalFieldManager vfmRS;
	private VerticalFieldManager vfmProKlaim;
	private String strMaxNo;
	private String strRecNo;
	private String strHurufAwal;
	private String strRootName;
	private BitmapField infoImage;
	
	private static final int _tab = 4;
	
	private String strInfoTitle;
	
	private VerticalFieldManager vfm;
	
	/**
     * Menambah Menu Close
     */
    protected void makeMenu( Menu menu, int instance ) {
		CommonMenu cm = new CommonMenu(this);
		menu.add(cm._welcomeScreen);
	}
	   
    /**
     * Action utk Back Button
     */
    protected boolean keyDown(int keycode, int status)  {
		if(Keypad.key(keycode) == Keypad.KEY_ESCAPE) {
			CommonMenu cm = new CommonMenu(this);
			if (intGetDetail != 1) {
				cm.welcomeScreen();
			} else {
				CallInfoServlet cis = new CallInfoServlet();
				cis.fnCallInfoServlet(this, intJnsInfo);
			}
			return true;
		}
		return false;
	}
	
    /**
     * Action Listener
     */
	public void fieldChanged(Field field, int context) {
		if (field == btHome) {
			CommonMenu cm = new CommonMenu(this);
			cm.welcomeScreen();
		} else if (field == btInfoRS) {
			fnViewInfoScreen(1);
		} else if (field == btInfoPersh) {
			fnViewInfoScreen(2);
		} else if (field == btInfoTesti) {
			fnViewInfoScreen(3);
		} else if (field == btInfoSimas) {
			fnViewInfoScreen(4);
		}
	}
	
	private void fnViewInfoScreen(int intJnsInfo) {
		CallInfoServlet cis = new CallInfoServlet();
		cis.fnCallInfoServlet(this, intJnsInfo);
	}
	
	public XmlShowINFOScreen(String strURL, int intJnsInfo) {
		
		Bitmap resizeBitmap = cf.getScaledBitmapImage(cr.getImageURL("bgSimasInfo.png"));
		Background bg = BackgroundFactory.createBitmapBackground(resizeBitmap, 
																 Background.POSITION_X_LEFT, 
																 Background.POSITION_Y_TOP, 
																 Background.REPEAT_NONE);

		vfm = new VerticalFieldManager(Field.USE_ALL_WIDTH);
		vfm.setBackground(bg);
		vfm.setFont(cr.getCustomFont(4));
		for (int a=1; a<=7; a++) {
			vfm.add(new LabelField(" ", Field.USE_ALL_WIDTH));
		}
		
		btHome = new CellButton("Home", Integer.parseInt("000000", 16), 
				 Integer.parseInt("FFCC33", 16), 
				 0, null, 0, 0, 0, 0);
		btHome.setFont(cr.getCustomFont());
		btHome.setChangeListener(this);
		
		btInfoRS = new CellButton("Rumah Sakit", Integer.parseInt("000000", 16), 
				 Integer.parseInt("FFCC33", 16), 
				 0, null, 0, 0, 0, 0);
		btInfoRS.setFont(cr.getCustomFont());
		btInfoRS.setChangeListener(this);
		
		btInfoPersh = new CellButton("Perusahaan", Integer.parseInt("000000", 16), 
				 Integer.parseInt("FFCC33", 16), 
				 0, null, 0, 0, 0, 0);
		btInfoPersh.setFont(cr.getCustomFont());
		btInfoPersh.setChangeListener(this);
		
		btInfoTesti = new CellButton("Testimonial", Integer.parseInt("000000", 16), 
				 Integer.parseInt("FFCC33", 16), 
				 0, null, 0, 0, 0, 0);
		btInfoTesti.setFont(cr.getCustomFont());
		btInfoTesti.setChangeListener(this);
		
//		btInfoSimas = new CellButton("Simas", Integer.parseInt("000000", 16), 
//				 Integer.parseInt("FFCC33", 16), 
//				 0, null, 0, 0, 0, 0);
//		btInfoSimas.setFont(cr.getCustomFont());
//		btInfoSimas.setChangeListener(this);
		
		lblSeparator1 = new LabelField(" | ", Field.FIELD_LEFT);
		lblSeparator1.setFont(cr.getCustomFont());
		
		lblSeparator2 = new LabelField(" | ", Field.FIELD_LEFT);
		lblSeparator2.setFont(cr.getCustomFont());
		
		lblSeparator3 = new LabelField(" | ", Field.FIELD_LEFT);
		lblSeparator3.setFont(cr.getCustomFont());
		
//		lblSeparator4 = new LabelField(" | ", Field.FIELD_LEFT);
//		lblSeparator4.setFont(cr.getCustomFont());
		
//		HorizontalFieldManager hfmButton = new HorizontalFieldManager(Field.FIELD_HCENTER);
//		hfmButton.add(btHome);
		//hfmButton.add(lblSeparator1);
		
		HorizontalFieldManager hfmButton1 = new HorizontalFieldManager(Field.FIELD_HCENTER);
		hfmButton1.add(btHome);
		hfmButton1.add(lblSeparator1);
		hfmButton1.add(btInfoRS);
		hfmButton1.add(lblSeparator2);
		hfmButton1.add(btInfoPersh);
		hfmButton1.add(lblSeparator3);
		hfmButton1.add(btInfoTesti);
//		hfmButton1.add(lblSeparator4);
//		hfmButton1.add(btInfoSimas);
		
//		vfm.add(hfmButton);
		vfm.add(hfmButton1);
		add(vfm);
		
		this.intJnsInfo = intJnsInfo;
		
		XMLWaitProcess xmlwp = new XMLWaitProcess();
		xmlwp.WaitProcess(strURL, "GET", this);
	}
	
	/**
	 * Request Gagal 
	 */
	public void requestFailed(final String message) {
		UiApplication.getUiApplication().invokeLater(new Runnable() {
			public void run() {
				Dialog.alert("Request failed. Reason: " + message);
			}
		});
	}
	
	/**
	 * Request Berhasil 
	 */
	public void requestSucceeded(byte[] result, String contentType) {
		if (contentType.startsWith("text/")) {
			synchronized (UiApplication.getEventLock()) {
				String strResult = new String(result);
				try {
					DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
		            DocumentBuilder builder = factory.newDocumentBuilder();
		            InputStream inputStream = new ByteArrayInputStream(strResult.getBytes());
		            Document document = builder.parse( inputStream );
		            
		            Element rootElement = document.getDocumentElement();
		            rootElement.normalize();
		            
		            if (intJnsInfo == 1) {
		            	// data rumah sakit
//			    		HorizontalFieldManager hfmTempatRawat = cr.getHeaderRibbon("DAFTAR TEMPAT PERAWATAN", 0);
//	        		    add(hfmTempatRawat);
		            	
		            	CustomButton btA = new CustomButton("A", Color.BLACK, Color.LIGHTGRAY, Color.YELLOW, Color.RED, 0);
	        			CustomButton btB = new CustomButton("B", Color.BLACK, Color.LIGHTGRAY, Color.YELLOW, Color.RED, 0);
	        			CustomButton btC = new CustomButton("C", Color.BLACK, Color.LIGHTGRAY, Color.YELLOW, Color.RED, 0);
	        			CustomButton btD = new CustomButton("D", Color.BLACK, Color.LIGHTGRAY, Color.YELLOW, Color.RED, 0);
	        			CustomButton btE = new CustomButton("E", Color.BLACK, Color.LIGHTGRAY, Color.YELLOW, Color.RED, 0);
	        			CustomButton btF = new CustomButton("F", Color.BLACK, Color.LIGHTGRAY, Color.YELLOW, Color.RED, 0);
	        			CustomButton btG = new CustomButton("G", Color.BLACK, Color.LIGHTGRAY, Color.YELLOW, Color.RED, 0);
	        			CustomButton btH = new CustomButton("H", Color.BLACK, Color.LIGHTGRAY, Color.YELLOW, Color.RED, 0);
	        			CustomButton btI = new CustomButton("I", Color.BLACK, Color.LIGHTGRAY, Color.YELLOW, Color.RED, 0);
	        			CustomButton btJ = new CustomButton("J", Color.BLACK, Color.LIGHTGRAY, Color.YELLOW, Color.RED, 0);
	        			CustomButton btK = new CustomButton("K", Color.BLACK, Color.LIGHTGRAY, Color.YELLOW, Color.RED, 0);
	        			CustomButton btL = new CustomButton("L", Color.BLACK, Color.LIGHTGRAY, Color.YELLOW, Color.RED, 0);
	        			CustomButton btM = new CustomButton("M", Color.BLACK, Color.LIGHTGRAY, Color.YELLOW, Color.RED, 0);
	        			CustomButton btN = new CustomButton("N", Color.BLACK, Color.LIGHTGRAY, Color.YELLOW, Color.RED, 0);
	        			CustomButton btO = new CustomButton("O", Color.BLACK, Color.LIGHTGRAY, Color.YELLOW, Color.RED, 0);
	        			CustomButton btP = new CustomButton("P", Color.BLACK, Color.LIGHTGRAY, Color.YELLOW, Color.RED, 0);
	        			CustomButton btQ = new CustomButton("Q", Color.BLACK, Color.LIGHTGRAY, Color.YELLOW, Color.RED, 0);
	        			CustomButton btR = new CustomButton("R", Color.BLACK, Color.LIGHTGRAY, Color.YELLOW, Color.RED, 0);
	        			CustomButton btS = new CustomButton("S", Color.BLACK, Color.LIGHTGRAY, Color.YELLOW, Color.RED, 0);
	        			CustomButton btT = new CustomButton("T", Color.BLACK, Color.LIGHTGRAY, Color.YELLOW, Color.RED, 0);
	        			CustomButton btU = new CustomButton("U", Color.BLACK, Color.LIGHTGRAY, Color.YELLOW, Color.RED, 0);
	        			CustomButton btV = new CustomButton("V", Color.BLACK, Color.LIGHTGRAY, Color.YELLOW, Color.RED, 0);
	        			CustomButton btW = new CustomButton("W", Color.BLACK, Color.LIGHTGRAY, Color.YELLOW, Color.RED, 0);
	        			CustomButton btX = new CustomButton("X", Color.BLACK, Color.LIGHTGRAY, Color.YELLOW, Color.RED, 0);
	        			CustomButton btY = new CustomButton("Y", Color.BLACK, Color.LIGHTGRAY, Color.YELLOW, Color.RED, 0);
	        			CustomButton btZ = new CustomButton("Z", Color.BLACK, Color.LIGHTGRAY, Color.YELLOW, Color.RED, 0);

	        			btA.setChangeListener(myListener);
	        			btB.setChangeListener(myListener);
	        			btC.setChangeListener(myListener);
	        			btD.setChangeListener(myListener);
	        			btE.setChangeListener(myListener);
	        			btF.setChangeListener(myListener);
	        			btG.setChangeListener(myListener);
	        			btH.setChangeListener(myListener);
	        			btI.setChangeListener(myListener);
	        			btJ.setChangeListener(myListener);
	        			btK.setChangeListener(myListener);
	        			btL.setChangeListener(myListener);
	        			btM.setChangeListener(myListener);
	        			btN.setChangeListener(myListener);
	        			btO.setChangeListener(myListener);
	        			btP.setChangeListener(myListener);
	        			btQ.setChangeListener(myListener);
	        			btR.setChangeListener(myListener);
	        			btS.setChangeListener(myListener);
	        			btT.setChangeListener(myListener);
	        			btU.setChangeListener(myListener);
	        			btV.setChangeListener(myListener);
	        			btW.setChangeListener(myListener);
	        			btX.setChangeListener(myListener);
	        			btY.setChangeListener(myListener);
	        			btZ.setChangeListener(myListener);

	        			HorizontalFieldManager hfmAlpha = new HorizontalFieldManager(Field.FIELD_HCENTER);
	        			hfmAlpha.setFont(cr.getCustomFont());
	        			hfmAlpha.add(btA);
	        			hfmAlpha.add(btB);
	        			hfmAlpha.add(btC);
	        			hfmAlpha.add(btD);
	        			hfmAlpha.add(btE);
	        			hfmAlpha.add(btF);
	        			hfmAlpha.add(btG);
	        			hfmAlpha.add(btH);
	        			hfmAlpha.add(btI);
	        			hfmAlpha.add(btJ);
	        			hfmAlpha.add(btK);
	        			hfmAlpha.add(btL);
	        			hfmAlpha.add(btM);
	        			
	        			HorizontalFieldManager hfmAlpha1 = new HorizontalFieldManager(Field.FIELD_HCENTER);
	        			hfmAlpha1.setFont(cr.getCustomFont());
	        			hfmAlpha1.add(btN);
	        			hfmAlpha1.add(btO);
	        			hfmAlpha1.add(btP);
	        			hfmAlpha1.add(btQ);
	        			hfmAlpha1.add(btR);
	        			hfmAlpha1.add(btS);
	        			hfmAlpha1.add(btT);
	        			hfmAlpha1.add(btU);
	        			hfmAlpha1.add(btV);
	        			hfmAlpha1.add(btW);
	        			hfmAlpha1.add(btX);
	        			hfmAlpha1.add(btY);
	        			hfmAlpha1.add(btZ);
	        			
	        			VerticalFieldManager hfmAlpha3 = new VerticalFieldManager(Field.USE_ALL_WIDTH);
	        			hfmAlpha3.setFont(cr.getCustomFont());
	        			hfmAlpha3.add(hfmAlpha);
	        			hfmAlpha3.add(hfmAlpha1);
	        			vfm.add(hfmAlpha3);
	        			
	        			displayNode( rootElement, 0 );
			            
	        	        HorizontalFieldManager btPrevNext = new HorizontalFieldManager(Field.USE_ALL_WIDTH);
	        	        btPrevNext.setFont(cr.getCustomFont());
	        	        btPrevList = new CustomButton(" << ", Color.BLACK, Color.LIGHTGRAY, Color.YELLOW, Color.RED, 0);
		        		btPrevList.setChangeListener(prevListener);
	            		btNextList = new CustomButton(" >> ", Color.BLACK, Color.LIGHTGRAY, Color.YELLOW, Color.RED, 0);
	            		btNextList.setChangeListener(nextListener);
	            		btPrevNext.add(btPrevList);
	        	        btPrevNext.add(new LabelField(" "));
		    			btPrevNext.add(btNextList);
		    			vfm.add(btPrevNext);

		            } else if (intJnsInfo == 2) {
		            	// info perusahaan
//		            	HorizontalFieldManager hfmProsedurKlaim = cr.getHeaderRibbon("INFO PERUSAHAAN", 0);
//	        		    add(hfmProsedurKlaim);
		            	
		            	vfm.add(new LabelField(" ", Field.USE_ALL_WIDTH));
	        		    
		            	displayNode( rootElement, 0 );
		            	
		            	//LabelField lblRecord = new LabelField(" " + strRecNo + "/" + strMaxNo + " ");
	        	        HorizontalFieldManager btPrevNext = new HorizontalFieldManager(Field.USE_ALL_WIDTH);
	        	        btPrevNext.setFont(cr.getCustomFont());
	        	        btPrevList = new CustomButton(" << ", Color.BLACK, Color.LIGHTGRAY, Color.YELLOW, Color.RED, 0);
		            	btPrevNext.setFont(cr.getCustomFont());
		        		btPrevList.setChangeListener(prevListener);
	            		btNextList = new CustomButton(" >> ", Color.BLACK, Color.LIGHTGRAY, Color.YELLOW, Color.RED, 0);
	            		btPrevNext.setFont(cr.getCustomFont());
	            		btNextList.setChangeListener(nextListener);
	            		if (intGetDetail != 1) {
	            			btPrevNext.add(btPrevList);
	            			btPrevNext.add(new LabelField(" "));
	            			btPrevNext.add(btNextList);
	            		}
		    			vfm.add(btPrevNext);
		    			
		    			if (intGetDetail == 1) {
		    				fnGetImage(strFoto);
		    			}
		            	
		            } else if (intJnsInfo == 3) {
		            	// testimonial
//		            	HorizontalFieldManager hfmProsedurKlaim = cr.getHeaderRibbon("TESTIMONI", 0);
//	        		    add(hfmProsedurKlaim);
	        		    
		            	vfm.add(new LabelField(" ", Field.USE_ALL_WIDTH));
		            	
		            	displayNode( rootElement, 0 );
		            	
		            	//LabelField lblRecord = new LabelField(" " + strRecNo + "/" + strMaxNo + " ");
	        	        HorizontalFieldManager btPrevNext = new HorizontalFieldManager(Field.USE_ALL_WIDTH);
	        	        btPrevNext.setFont(cr.getCustomFont());
	        	        btPrevList = new CustomButton(" << ", Color.BLACK, Color.LIGHTGRAY, Color.YELLOW, Color.RED, 0);
		        		btPrevList.setChangeListener(prevListener);
	            		btNextList = new CustomButton(" >> ", Color.BLACK, Color.LIGHTGRAY, Color.YELLOW, Color.RED, 0);
	            		btNextList.setChangeListener(nextListener);
	            		
	            		if (intGetDetail != 1) {
	            			btPrevNext.add(btPrevList);
	            			btPrevNext.add(new LabelField(" "));
	            			btPrevNext.add(btNextList);
	            		}
		    			vfm.add(btPrevNext);
		    			
		    			if (intGetDetail == 1) {
		    				fnGetImage(strFoto);
		    			}
		    			
		            } else if (intJnsInfo == 4) {
		            	// info asuransi
//		            	HorizontalFieldManager hfmProsedurKlaim = cr.getHeaderRibbon("INFO ASURANSI", 0);
//	        		    add(hfmProsedurKlaim);
	        		    
		            	vfm.add(new LabelField(" ", Field.USE_ALL_WIDTH));
		            	
		            	displayNode( rootElement, 0 );
		            	
		            	//LabelField lblRecord = new LabelField(" " + strRecNo + "/" + strMaxNo + " ");
	        	        HorizontalFieldManager btPrevNext = new HorizontalFieldManager(Field.USE_ALL_WIDTH);
	        	        btPrevNext.setFont(cr.getCustomFont());
		            	btPrevList = new CustomButton(" << ", Color.BLACK, Color.LIGHTGRAY, Color.YELLOW, Color.RED, 0);
		        		btPrevList.setChangeListener(prevListener);
	            		btNextList = new CustomButton(" >> ", Color.BLACK, Color.LIGHTGRAY, Color.YELLOW, Color.RED, 0);
	            		btNextList.setChangeListener(nextListener);
	            		if (intGetDetail != 1) {
	            			btPrevNext.add(btPrevList);
	            			btPrevNext.add(new LabelField(" "));
	            			btPrevNext.add(btNextList);
	            		}
		    			vfm.add(btPrevNext);
		    			
		    			if (intGetDetail == 1) {
		    				fnGetImage(strFoto);
		    			}
		    			
		            }
				}
				catch (Exception exp) {
					// nothing
					Dialog.alert(exp.toString());
				}
			}
		} else {
			synchronized (UiApplication.getEventLock()) {
				Dialog.alert("Unknown content type: " + contentType);
			}
		}
	}
	
	/**
	 * membaca result berupa xml
	 */
	private void displayNode( Node node, int depth ) {
		
		// ==> element artinya diapit oleh xml-tag, misal <nama>novan</nama>
        if ( node.getNodeType() == Node.ELEMENT_NODE ) {
            
        	StringBuffer buffer = new StringBuffer();
            indentStringBuffer( buffer, depth );
        	NodeList childNodes = node.getChildNodes();
            int numChildren = childNodes.getLength();
            Node firstChild = childNodes.item( 0 );
            
            if ( numChildren == 1 && firstChild.getNodeType() == Node.TEXT_NODE ) {
            	if (strRootName.equalsIgnoreCase("InfoRumahSakit")) {
            		
            		vfmRS = new VerticalFieldManager(Field.USE_ALL_WIDTH);
            		vfmRS.setFont(cr.getCustomFont());

            		if (node.getNodeName().equalsIgnoreCase("NamaRS")) {
//            			HorizontalFieldManager hfmNamaRS = new HorizontalFieldManager(Field.USE_ALL_WIDTH){
//            	            public void paint(Graphics graphics){
//            	                graphics.setBackgroundColor(Integer.parseInt("663300", 16));
//            	                graphics.clear();
//            	                super.paint(graphics);
//            	            }
//            	        };
//            	        LabelField lblNamaRS = cr.myCustomLabel(firstChild.getNodeValue(), 6, Field.USE_ALL_WIDTH);
//            	        hfmNamaRS.add(lblNamaRS);
            			HorizontalFieldManager hfmNamaRS = cr.getHeaderRibbon(firstChild.getNodeValue(), 5);
            			vfmRS.add(hfmNamaRS);
            		} else if (node.getNodeName().equalsIgnoreCase("AlamatRS")) {
            			vfmRS.add(new RichTextField(firstChild.getNodeValue(), Field.FIELD_LEFT));
            		} else if (node.getNodeName().equalsIgnoreCase("TelpRS")) {
            			vfmRS.add(new RichTextField("Telp: " + firstChild.getNodeValue(), Field.FIELD_LEFT));
            		} else if (node.getNodeName().equalsIgnoreCase("FaxRS")) {
            			vfmRS.add(new RichTextField("Faks: " + firstChild.getNodeValue(), Field.FIELD_LEFT));
            		} else if (node.getNodeName().equalsIgnoreCase("StsProvider")) {
            			vfmRS.add(new RichTextField("Status: " + firstChild.getNodeValue(), Field.FIELD_LEFT));
            			vfmRS.add(new RichTextField(" ", Field.FIELD_LEFT));
            		} else if (node.getNodeName().equalsIgnoreCase("HurufAwal")) {
            			strHurufAwal = firstChild.getNodeValue();
            		} else if (node.getNodeName().equalsIgnoreCase("MaxNo")) {
            			strMaxNo = firstChild.getNodeValue();
            		} else if (node.getNodeName().equalsIgnoreCase("RecNo")) {
            			strRecNo = firstChild.getNodeValue();
            		}
		    		
            		vfm.add(vfmRS);
	            		
            	} else if (strRootName.equalsIgnoreCase("InfoPerusahaan")) {
            		
            		vfmProKlaim = new VerticalFieldManager(Field.USE_ALL_WIDTH);
		    		vfmProKlaim.setFont(cr.getCustomFont());
		    		
		    		if (node.getNodeName().equalsIgnoreCase("InfoId")) {
            		} else if (node.getNodeName().equalsIgnoreCase("InfoTitle")) {
            			btInfoTitle = new RowButton(firstChild.getNodeValue(), Color.WHITE, Integer.parseInt("CC9966", 16), Color.YELLOW, Color.RED, RowButton.USE_ALL_WIDTH);
            			if (intGetDetail != 1) {
            				btInfoTitle.setChangeListener(infoRowListener);
            			}
            			vfmProKlaim.add(btInfoTitle);
            		} else if (node.getNodeName().equalsIgnoreCase("InfoBody")) {
            			RichTextField rtfBody = new RichTextField(firstChild.getNodeValue(), Field.FIELD_LEFT);
            			rtfBody.setFont(cr.getCustomFont());
            			vfmProKlaim.add(rtfBody);
            			vfmProKlaim.add(new RichTextField(" ", Field.FIELD_LEFT));
            		} else if (node.getNodeName().equalsIgnoreCase("InfoEntryDate")) {
//            			vfmProKlaim.add(new RichTextField(" ", Field.FIELD_LEFT));
//            			gfmProKlaim.add(new RichTextField("Tgl Posting: " + firstChild.getNodeValue(), Field.FIELD_LEFT));
            		} else if (node.getNodeName().equalsIgnoreCase("InfoContributor")) {
//            			gfmProKlaim.add(new RichTextField("Editor: " + firstChild.getNodeValue(), Field.FIELD_LEFT));
//            			vfmProKlaim.add(gfmProKlaim);
//            			gfmProKlaim = new GridFieldManager(2,0);
//            			vfmProKlaim.add(new RichTextField(" ", Field.FIELD_LEFT));
            		} else if (node.getNodeName().equalsIgnoreCase("MaxNo")) {
            			strMaxNo = firstChild.getNodeValue();
//            		} else if (node.getNodeName().equalsIgnoreCase("RecNo")) {
            			strRecNo = firstChild.getNodeValue();
            		} else if (node.getNodeName().equalsIgnoreCase("Detail")) {
            			intGetDetail = Integer.parseInt(firstChild.getNodeValue());
            		} else if (node.getNodeName().equalsIgnoreCase("Foto")) {
            			strFoto = firstChild.getNodeValue();
            			if (intGetDetail == 1) {
            				Bitmap bitmapImage = null;
            				infoImage = new BitmapField(bitmapImage,Field.FIELD_HCENTER);
            				HorizontalFieldManager imageHField = new HorizontalFieldManager(Field.FIELD_HCENTER);
            				imageHField.add(infoImage);
            				
            				VerticalFieldManager imageVField = new VerticalFieldManager(Field.USE_ALL_WIDTH);
            				imageVField.add(imageHField);
            				
            				vfmProKlaim.add(new RichTextField(" ", Field.FIELD_LEFT));
            				vfmProKlaim.add(imageVField);
            				vfmProKlaim.add(new RichTextField(" ", Field.FIELD_LEFT));
            			}
            		}
            		
            		vfm.add(vfmProKlaim);
            		
            	} else if (strRootName.equalsIgnoreCase("Testimonial")) {;

            		vfmProKlaim = new VerticalFieldManager(Field.USE_ALL_WIDTH);
		    		vfmProKlaim.setFont(cr.getCustomFont());
            		
		    		if (node.getNodeName().equalsIgnoreCase("InfoId")) {
		    			
            		} else if (node.getNodeName().equalsIgnoreCase("InfoTitle")) {
//            			btInfoTitle = new RowButton(firstChild.getNodeValue(), Color.BLACK, Integer.parseInt("FFE4C4", 16), Color.YELLOW, Color.GREEN, RowButton.USE_ALL_WIDTH);
//            			if (intGetDetail != 1) {
//            				btInfoTitle.setChangeListener(infoRowListener);
//            			}
//            			vfmProKlaim.add(btInfoTitle);
//            			vfmProKlaim.add(new RichTextField(" ", Field.FIELD_LEFT));
            			
            			btInfoTitle = new RowButton(firstChild.getNodeValue(), Color.WHITE, Integer.parseInt("CC9966", 16), Color.YELLOW, Color.RED, RowButton.USE_ALL_WIDTH);
            			if (intGetDetail != 1) {
            				btInfoTitle.setChangeListener(infoRowListener);
            			}
            			vfmProKlaim.add(btInfoTitle);
            		} else if (node.getNodeName().equalsIgnoreCase("InfoBody")) {
//            			vfmProKlaim.add(new RichTextField(firstChild.getNodeValue(), Field.FIELD_LEFT));
//            			vfmProKlaim.add(new RichTextField(" ", Field.FIELD_LEFT));
            			
            			RichTextField rtfBody = new RichTextField(firstChild.getNodeValue(), Field.FIELD_LEFT);
            			rtfBody.setFont(cr.getCustomFont());
            			vfmProKlaim.add(rtfBody);
            			vfmProKlaim.add(new RichTextField(" ", Field.FIELD_LEFT));
            		} else if (node.getNodeName().equalsIgnoreCase("InfoEntryDate")) {
//            			vfmProKlaim.add(new RichTextField("Tgl Posting: " + firstChild.getNodeValue(), Field.FIELD_LEFT));
            		} else if (node.getNodeName().equalsIgnoreCase("InfoContributor")) {
//            			vfmProKlaim.add(new RichTextField("Editor: " + firstChild.getNodeValue(), Field.FIELD_LEFT));
            		} else if (node.getNodeName().equalsIgnoreCase("MaxNo")) {
            			strMaxNo = firstChild.getNodeValue();
            		} else if (node.getNodeName().equalsIgnoreCase("RecNo")) {
            			strRecNo = firstChild.getNodeValue();
            		} else if (node.getNodeName().equalsIgnoreCase("Detail")) {
            			intGetDetail = Integer.parseInt(firstChild.getNodeValue());
            		} else if (node.getNodeName().equalsIgnoreCase("Foto")) {
            			strFoto = firstChild.getNodeValue();
            			if (intGetDetail == 1) {
            				Bitmap bitmapImage = null;
            				infoImage = new BitmapField(bitmapImage,Field.FIELD_HCENTER);
            				HorizontalFieldManager imageHField = new HorizontalFieldManager(Field.FIELD_HCENTER);
            				imageHField.add(infoImage);
            				
            				VerticalFieldManager imageVField = new VerticalFieldManager(Field.USE_ALL_WIDTH);
            				imageVField.add(imageHField);
            				
            				vfmProKlaim.add(new RichTextField(" ", Field.FIELD_LEFT));
            				vfmProKlaim.add(imageVField);
            				vfmProKlaim.add(new RichTextField(" ", Field.FIELD_LEFT));
            			}
            		}
            		
            		vfm.add(vfmProKlaim);
            		
            	} else if (strRootName.equalsIgnoreCase("InfoAsuransi")) {
            		
            		vfmProKlaim = new VerticalFieldManager(Field.USE_ALL_WIDTH);
		    		vfmProKlaim.setFont(cr.getCustomFont());
		    		
		    		if (node.getNodeName().equalsIgnoreCase("InfoId")) {
		    			
            		} else if (node.getNodeName().equalsIgnoreCase("InfoTitle")) {
//            			btInfoTitle = new RowButton(firstChild.getNodeValue(), Color.BLACK, Integer.parseInt("FFE4C4", 16), Color.YELLOW, Color.GREEN, RowButton.USE_ALL_WIDTH);
//            			if (intGetDetail != 1) {
//            				btInfoTitle.setChangeListener(infoRowListener);
//            			}
//            			vfmProKlaim.add(btInfoTitle);
//            			vfmProKlaim.add(new RichTextField(" ", Field.FIELD_LEFT));
            			
            			btInfoTitle = new RowButton(firstChild.getNodeValue(), Color.WHITE, Integer.parseInt("CC9966", 16), Color.YELLOW, Color.RED, RowButton.USE_ALL_WIDTH);
            			if (intGetDetail != 1) {
            				btInfoTitle.setChangeListener(infoRowListener);
            			}
            			vfmProKlaim.add(btInfoTitle);
            		} else if (node.getNodeName().equalsIgnoreCase("InfoBody")) {
//            			vfmProKlaim.add(new RichTextField(firstChild.getNodeValue(), Field.FIELD_LEFT));
//            			vfmProKlaim.add(new RichTextField(" ", Field.FIELD_LEFT));
            			
            			RichTextField rtfBody = new RichTextField(firstChild.getNodeValue(), Field.FIELD_LEFT);
            			rtfBody.setFont(cr.getCustomFont());
            			vfmProKlaim.add(rtfBody);
            			vfmProKlaim.add(new RichTextField(" ", Field.FIELD_LEFT));
            		} else if (node.getNodeName().equalsIgnoreCase("InfoEntryDate")) {
//            			vfmProKlaim.add(new RichTextField("Tgl Posting: " + firstChild.getNodeValue(), Field.FIELD_LEFT));
            		} else if (node.getNodeName().equalsIgnoreCase("InfoContributor")) {
//            			vfmProKlaim.add(new RichTextField("Editor: " + firstChild.getNodeValue(), Field.FIELD_LEFT));
            		} else if (node.getNodeName().equalsIgnoreCase("MaxNo")) {
            			strMaxNo = firstChild.getNodeValue();
            		} else if (node.getNodeName().equalsIgnoreCase("RecNo")) {
            			strRecNo = firstChild.getNodeValue();
            		} else if (node.getNodeName().equalsIgnoreCase("Detail")) {
            			intGetDetail = Integer.parseInt(firstChild.getNodeValue());
            		} else if (node.getNodeName().equalsIgnoreCase("Foto")) {
            			strFoto = firstChild.getNodeValue();
            			if (intGetDetail == 1) {
            				Bitmap bitmapImage = null;
            				infoImage = new BitmapField(bitmapImage,Field.FIELD_HCENTER);
            				HorizontalFieldManager imageHField = new HorizontalFieldManager(Field.FIELD_HCENTER);
            				imageHField.add(infoImage);
            				
            				VerticalFieldManager imageVField = new VerticalFieldManager(Field.USE_ALL_WIDTH);
            				imageVField.add(imageHField);
            				
            				vfmProKlaim.add(new RichTextField(" ", Field.FIELD_LEFT));
            				vfmProKlaim.add(imageVField);
            				vfmProKlaim.add(new RichTextField(" ", Field.FIELD_LEFT));
            			}
            		}
            		
            		vfm.add(vfmProKlaim);
            		
            	}
            	
            } else if (numChildren > 1) {
            	
                // ==> lebih dari 1 children artinya ada lebih dari satu sub-element
                //buffer.append( node.getNodeName() );
                //add( new RichTextField( buffer.toString() ) ); // dimatikan supaya tidak menuliskan nama node -nya
            	strRootName = node.getNodeName();
            	
            	// Recursively visit all this node's children.
                // ==> rekursif, panggil diri sendiri untuk menampilkan isinya
                for ( int i = 0; i < numChildren; ++i ) {
                	displayNode( childNodes.item( i ), depth + 1 );
                }
                
            } else if (numChildren == 0)  {
            	for ( int i = 0; i < numChildren; ++i ) {
            		displayNode( childNodes.item( i ), depth + 1 );
                }
            }
            
        } else {
        	
            // ==> bukan element, artinya tidak diapit oleh xml-tag.. berarti ini text-node
            // ==> pada aplikasi ini, tidak akan ada text-node.. karena semua akan memakai tag..
        	// ==> jadi sebetulnya bagian ini bisa dihapus..
            String nodeValue = node.getNodeValue();
            if ( nodeValue.trim().length() != 0 )  {
                StringBuffer buffer = new StringBuffer();
                indentStringBuffer( buffer, depth );
                buffer.append( '"' ).append( nodeValue ).append( '"' );
                add(new LabelField(buffer.toString(), Field.USE_ALL_WIDTH));
            }
            
        }
    }
	
	/**
	 * Untuk Keperluan baca XML
	 */
	private static void indentStringBuffer( StringBuffer buffer, int depth ) {
        int indent = depth * _tab;
        for ( int i = 0; i < indent; ++i ) {
            buffer.append( ' ' );
        }
    }
	
	/**
     * Action Listener btPrev
     */
    FieldChangeListener prevListener = new FieldChangeListener() {
	     public void fieldChanged(Field field, int context) {
	    	 int intNewNum = Integer.parseInt(strRecNo) - 19;
	    	 int intMaxNo = Integer.parseInt(strMaxNo);
	    	 if (intNewNum < 1) {
	    		 intNewNum = 1;
	    	 }
           strRecNo = String.valueOf(intNewNum);
           strMaxNo = String.valueOf(intMaxNo);
           fnViewKlaimScreen();
       }
	};
	
	/**
     * Action Listener btNext
     */
	FieldChangeListener nextListener = new FieldChangeListener() {
	     public void fieldChanged(Field field, int context) {
	    	 int intNewNum = Integer.parseInt(strRecNo) + 10;
	    	 int intMaxNo = Integer.parseInt(strMaxNo);
	    	 if (intNewNum > intMaxNo) {
	    		 intNewNum = intMaxNo - 9;
	    		 if (intNewNum < 0) {
	    			 intNewNum = 1;
	    		 }
	    	 } else if (intNewNum == intMaxNo) {
	    		 intNewNum = Integer.parseInt(strRecNo) + 9;
	    	 } else if (intNewNum < intMaxNo) {
	    		 intNewNum = Integer.parseInt(strRecNo) + 9;
	    	 }
	    	 
           strRecNo = String.valueOf(intNewNum);
           strMaxNo = String.valueOf(intMaxNo);
           fnViewKlaimScreen();
       }
	};
	
	/**
	 * view detail info
	 */
	private void fnViewKlaimScreen() {
		int intHidMode = 1;
		if (intJnsInfo == 2) {
			intHidMode = 2;
		} else if (intJnsInfo == 3) {
			intHidMode = 3;
		} else if (intJnsInfo == 4) {
			intHidMode = 4;
		}
		CallInfoServlet cis = new CallInfoServlet();
//		Dialog.alert("strRecNo: " + strRecNo + "\nstrMaxNo: " + strMaxNo);
		cis.fnCallInfoServlet(this, intHidMode, strHurufAwal, strRecNo, strMaxNo, strInfoTitle, intGetDetail);
	}
	
	/**
	 * Action Listener btFont
	 */
	FieldChangeListener myListener = new FieldChangeListener() {
	     public void fieldChanged(Field field, int context) {
	    	 CustomButton cusButton = (CustomButton) field;
	    	 strRecNo = "1";
	    	 strHurufAwal = cusButton.getLabel();
	         fnViewKlaimScreen();
	     }
	};
	
	/**
	 * Action Listener btRowTitle
	 */
	FieldChangeListener infoRowListener = new FieldChangeListener() {
	     public void fieldChanged(Field field, int context) {
	    	 RowButton btInfoTitle1 = (RowButton) field;
	    	 strRecNo = "1";
	    	 strInfoTitle = btInfoTitle1.getLabel();
	    	 intGetDetail = 1;
	    	 fnViewKlaimScreen();
	     }
	};
	
	/**
	 * Retrieve Image for specific info 
	 */
	private void fnGetImage(String strFoto) {
//		Dialog.alert("fnGetImage!");
		CallInfoServlet cis = new CallInfoServlet();
		int intHidMode = 5;
		//String strURL = cf.fnStringToURL("http://192.168.10.130:8083/SimasServer/kereta2.jpg"); 
		String strURL = cf.fnStringToURL(cis.fnRetrieveImageURL(intHidMode, strFoto));
//		Dialog.alert("strURL: " + strURL);
		NoWaitProcess nwp = new NoWaitProcess(strURL, "GET", this);
		nwp.start();
	}
	
	/**
	 * If image retrieved
	 */
	public void imageRetrieved(byte[] result, String contentType) {
		if (contentType.equals("image/png") ||
			contentType.equals("image/jpeg") ||
			contentType.equals("image/gif")) {
			
			Bitmap bitmap = Bitmap.createBitmapFromBytes(result, 0, result.length, 2);
			synchronized (UiApplication.getEventLock()) {
				infoImage.setBitmap(bitmap);
			}
		}
	}
}
